<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="model.database.DBException"%>
<%@page import="controller.CustomerController"%>
<%@page import="controller.CmnTransactionsCtrl"%>
<%@page import="view.WebCommons"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="orderData" class="view.useBeans.OrderData" scope="page" />
<jsp:setProperty name="orderData" property="*" />

<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Make new order</title>
</head>
<body>

	<span class="customHeader2">Make order</span>

	<%
		// extract customer number from session	
		Integer customerNumber = (Integer) session
				.getAttribute(WebCommons.CUSTOMER_NUMBER);

		//if (!orderData.isDataSet()) { // display form for first time on page
	%>

		<!-- get new order's details -->
	
		<div id="page">
			<div class="post">
	
				<div id="sidebar">
					<ul>
						<li>
							<h2>Please insert new order's details</h2>
						</li>
					</ul>
				</div>
				<div id="content">
					<form METHOD=POST ACTION="MakeOrder.jsp">
						<table>
							<tr>
								<td><span class="customFormValue">Ordering customer number</span></td>
								<%
									// check if uses customer number from session or should ask for one
										if (customerNumber == null) {
								%>
								<td><INPUT TYPE=TEXT NAME=orderingCustomerNumber
									SIZE=<%=WebCommons.NUMBER_SIZE%>></td>
								<%
									} else {
								%>
								<td><INPUT TYPE=TEXT NAME=orderingCustomerNumber
									VALUE=<%=customerNumber%> SIZE=<%=WebCommons.NUMBER_SIZE%>
									readonly="readonly" class="formROdata"></td>
								<%
									}
								%>
							</tr>
							<tr>
								<td><span class="customFormValue">Name of product to
										order</span></td>
								<td><INPUT TYPE=TEXT NAME=orderedProductName
									SIZE=<%=WebCommons.NAME_SIZE%>></td>
							</tr>
							<tr>
								<td><span class="customFormValue">How many items to
										order?</span></td>
								<td><INPUT TYPE=TEXT NAME=amount
									SIZE=<%=WebCommons.NUMBER_SIZE%>></td>
							</tr>
						</table>
						<br> <INPUT TYPE=SUBMIT VALUE="Preview order"
							class="mySubmitButton">
					</form>
				</div>
			</div>
		</div>

		<%
		// return to main menu
			String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
		%>
		<div id="footer">
			<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back
					to Menu</A></span>
		</div>

		<%
		
		// check if already submitted details
		//} else {
		if (orderData.isDataSet()) {

			// extract given details
			customerNumber = orderData.getCustomerNumberValue();
			String product = orderData.getOrderedProductName();
			Integer amount = orderData.getAmountValue();
			Double price = null; // will be set according to price stored in system

			boolean customerExists = true; // indicates if ordering customer number matches a customer in system
			boolean productExists = true; // indicates if ordered product matches a product in system
			boolean canOrder = true; // indicates if can make order according to given data

			// validate data
			if (orderData.isNumericDataValid()) {

				// extract controller from session				
				CustomerController controller = (CustomerController) session.getAttribute(WebCommons.CONTROLLER);

				try {

					// check if customer exists
					Map<String, String> customerDetails = controller.getCustomerDetails(customerNumber);
					if (customerDetails.isEmpty()) {

						// no such customer
						customerExists = false;
						canOrder = false;
					}

					// check if product exist
					price = controller.getProductPrice(product);
					if (Double.isNaN(price)) {

						// product does not exist
						productExists = false;
						canOrder = false;
					}

				} catch (DBException e) {

					// move to error page
					response.sendRedirect("../../commonPages/ErrorPage.jsp");
				}

				// if can, make order
				if (canOrder) {

					// keep order data instance
					request.setAttribute(WebCommons.ORDER_DATA, orderData);

					// move to confirm-order page
					request.getRequestDispatcher("ConfirmOrder.jsp").forward(request, response);
				}
			}


			// errors in numeric data received, display matching error message
	
			// verify customer number's format
			if (customerNumber == null) {
				%>
				<span class="notify"><br>Please enter a numeric value for customer number</span>
				<%
			} else if (!customerExists) { // error message for customer 

				// customer number data is numeric, but number is not in system
				%>
				<span class="notify"><br>Sorry, no customer matching given number</span>
				<%
			}

			// error message for amount
			if (amount == null) {
				%>
				<span class="notify"><br>Please enter a positive integer number for ordered amount</span>
				<%
			}

			// error message for product
			if (!productExists) {
				%>
				<span class="notify"><br>Sorry, no product matching given name</span>
				<%
			}
		}
	%>

</body>
</html>